local helper=require 'library.helpers'
local db = require 'models.db'
local user = {}
--require ('library.sha1.Sha1')

function user:check(username,passwd,remember)
	local usertable = db:new('users')
	local useritem=usertable:where("name=?",username):get_one()
	if not useritem then
		return 1
	end		

	if helper:sha1(passwd)==useritem['password'] then
		self:success(useritem)
		return 0
	end
	
	return 2
end

function user:success(useritem)
	local session=helper:get_session()
	session.data.user=useritem
end

function user:is_login()
	local session=helper:get_session()
	if session.data.user and session.data.user['id'] then
		return true
	end
	return false
end

function user:get()
	local session=helper:get_session()
	return session.data.user
end

function user:logout()
	local session=helper:get_session()
	session.data.user=nil
	session:save()
end

return user